// components/filters/filters.js
const app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    dropDownMenuTitle: {
      type: Array,
      value: ['1', '2', '3'],
    },
    dropDownMenuSecondData: {
      type: Array,
      value: []
    },
    dropDownMenuThirdData: {
      type: Array,
      value: []
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
     // 这里是一些组件内部数据
    filterTop:app.globalData.status+app.globalData.navHeight+45,
    zhOpen:false,        //综合排序
    cateOpen: false,     //产品分类
    tradeOpen: false,    //行业应用
    shownavindex: '',
    dropDownMenuDataSecondRight: {},
    select1:1,
    select2: 11,
    select3: 111,
    selectedTrade: ''
  }, 

  /**
   * 组件的方法列表
   */
  methods: {
    //综合筛选
    zhFilter(e){ 
      if (this.data.zhOpen) {
        this.closeHyFilter();
        this.setData({
          shownavindex: 0
        })
      } else {
        this.setData({
          zhOpen: true,
          cateOpen: false,
          tradeOpen: false,
          shownavindex: e.currentTarget.dataset.nav
        })
      }
    },
    //产品分类
    bindCate(e){
      if (this.data.cateOpen) {
        this.closeHyFilter();
        this.setData({
          shownavindex: 0
        })
      } else {
        this.setData({
          cateOpen: true,
          zhOpen: false,
          tradeOpen: false,
          shownavindex: e.currentTarget.dataset.nav
        })
      }
    },
    //行业分类
    bindTrade(e){
      if (this.data.tradeOpen) {
        this.closeHyFilter();
        this.setData({
          shownavindex: 0
        })
      } else {
        this.setData({
          tradeOpen: true,
          cateOpen: false,
          zhOpen: false,
          shownavindex: e.currentTarget.dataset.nav
        })
      }
    },
    selectleft: function(e) {
      var model = e.target.dataset.model.childModel;
      var selectedId = e.target.dataset.model.id
      var selectedTitle = e.target.dataset.model.title;
      this.setData({
        dropDownMenuDataSecondRight: model == null ? "" : model,
        select1: selectedId,
        select2: '',
      })
      if (model == null || model.length == 0) {
        this.closeHyFilter();
        this.triggerEvent("selectedItem", {
          index: this.data.shownavindex,
          selectedId: selectedId,
          selectedTitle: selectedTitle
        })
      }
    },
    selectcenter: function(e) {
      console.log('e',e);
      const {
        parentIndex,
        item,
        index
      } = e.currentTarget.dataset
      // 如果选中状态
      if (item.lastModel[index].checked) {
        item.lastModel[index].checked = false // 取消选择
      } else {
        // 如果不是多选
        if (item.type != 'checkbox') {
          // 全部重置为未选择状态
          item.lastModel.map(n => (n.checked = false))
        }
        // 将点击的设置为选中
        item.lastModel[index].checked = true
      }
      this.setData({
        [`dropDownMenuDataSecondRight[${parentIndex}]`]: item
      },function(){//更新选中值
        let selected = []
        // 获取所有选中
        this.data.dropDownMenuDataSecondRight.map(n => {
          n.lastModel.map(m => {
            if (m.checked == true) {
              selected.push(m.title)
            }
          })
        })
        console.log('selected',selected);
      })






    },
    selectradeitem(e){
      var selectedId = e.target.dataset.model.id
      var selectedTitle = e.target.dataset.model.title;
      this.closeHyFilter();
      this.setData({
        selectedTrade: selectedId
      })
      this.triggerEvent("selectedItem", {
        index: this.data.shownavindex,
        selectedId: selectedId,
        selectedTitle: selectedTitle
      })
    },
    /**关闭筛选 */
    closeHyFilter: function() {
      this.setData({
        cateOpen: false,
        zhOpen: false,
        tradeOpen: false,
      })
    },
    close() {
      this.closeHyFilter()
    },
  },
  //组件生命周期函数，在组件实例进入页面节点树时执行
  attached: function() {
    // 可以在这里发起网络请求获取插件的数据
    var secondData = this.data.dropDownMenuSecondData[this.data.select1-1].childModel
    console.log('secondData',secondData);
    this.setData({
      dropDownMenuDataSecondRight: secondData == null ? "" : secondData,
    })
    var self = this
    wx.getSystemInfo({
      success: function (res) {
        self.setData({
          userHeight: res.windowHeight - 41,
        })
      }
    })
  },
})
